#!/user/bin/env python
#coding:utf-8
import wx
import os
import tool
class FileDropTarget(wx.FileDropTarget):
    def __init__(self, window):
          wx.FileDropTarget.__init__(self)
          self.window = window

    def OnDropFiles(self,  x,  y, fileNames):
          self.window.SetValue(str(fileNames))

class TaskBarIcon(wx.TaskBarIcon):
    ID_Play = wx.NewId()
    ID_About = wx.NewId()
    ID_Minshow=wx.NewId()
    ID_Maxshow=wx.NewId()
    ID_Closeshow=wx.NewId()
    ID_WinShow=wx.NewId()
    def __init__(self, frame):
        wx.TaskBarIcon.__init__(self)
        self.frame = frame
        self.SetIcon(wx.Icon(name='icon.ico', type=wx.BITMAP_TYPE_ICO), 'GameOptions!')
        self.Bind(wx.EVT_TASKBAR_LEFT_DCLICK, self.OnTaskBarLeftDClick)
        self.Bind(wx.EVT_MENU, self.Winshow, id=self.ID_WinShow)
        self.Bind(wx.EVT_MENU, self.OnAbout, id=self.ID_About)
        self.Bind(wx.EVT_MENU, self.OnMinshow, id=self.ID_Minshow)
        #self.Bind(wx.EVT_MENU, self.OnMaxshow, id=self.ID_Maxshow)
        self.Bind(wx.EVT_MENU, self.OnCloseshow, id=self.ID_Closeshow)

    def OnTaskBarLeftDClick(self, event):
        if self.frame.IsIconized():
           self.frame.Iconize(False)
        if not self.frame.IsShown():
           self.frame.Show(True)
        self.frame.Raise()

    def Winshow(self, event):
        self.frame.Iconize(True)
        self.frame.Show(True)
        self.frame.Raise()
    def OnAbout(self,event):
        wx.MessageBox('Something of Nothing!', 'About')

    def OnMinshow(self,event):
        self.frame.Iconize(True)


    def OnMaxshow(self,event):
        if self.frame.IsIconized():
           self.frame.Iconize(False)
        if not self.frame.IsShown():
           self.frame.Show(True)
        self.frame.Raise()
        self.frame.Maximize(True)

    def OnCloseshow(self,event):
        self.frame.Close(True)

    # 右键菜单
    def CreatePopupMenu(self):
        menu = wx.Menu()
        menu.Append(self.ID_WinShow, 'Show')
        menu.Append(self.ID_Minshow, 'Small')
        #menu.Append(self.ID_Maxshow, 'Big')
        menu.Append(self.ID_About, 'About')
        menu.Append(self.ID_Closeshow, 'Exit')
        return menu
class App(wx.App):
    def __init__(self):
        super(self.__class__, self).__init__()

    def OnInit(self):
        frame = MyFram()
        #frame2=AddGame()
        frame.Show(True)
        self.SetTopWindow(frame)

        return True

class Del_Game(wx.Frame):
    def __init__(self):
        super(self.__class__, self).__init__(parent = None, id = -1, title = "GameOptions", size = (300, 500))
        self.SetMaxSize((300, 500))
        self.panel = wx.Panel(parent = self)
    def _Return(self):
        pass
class Add_Game(wx.Frame):
    def __init__(self):
        super(self.__class__, self).__init__(parent = None, id = -1, title = "GameOptions", size = (500, 200))
        self.SetMaxSize((300, 500))
        self.panel = wx.Panel(parent = self)
    def _Return(self):
        pass

class Bilibili_Live_recorded(wx.Frame):
    def __init__(self):
        super(self.__class__, self).__init__(parent = None, id = -1, title = "GameOptions", size = (500, 200))
        self.SetMaxSize((500, 200))
        self.panel = wx.Panel(parent = self)
    def _Return(self):
        pass


class MyFram(wx.Frame):
    def __init__(self):
        super(self.__class__, self).__init__(parent = None, id = -1, title = "GameOptions", size = (300, 500))
        self.Menu_A_DD = wx.NewId()
        self.Menu_Bili = wx.NewId()
        self.SetMaxSize((300, 500))
        self.SetIcon(wx.Icon('icon.ico', wx.BITMAP_TYPE_ICO))
        self.taskBarIcon = TaskBarIcon(self)
        self.Bind(wx.EVT_CLOSE, self.OnClose)
        self.Bind(wx.EVT_ICONIZE, self.OnIconfiy)
        self.panel = wx.Panel(parent = self)
        self.panel.Bind(wx.EVT_ERASE_BACKGROUND,self.OnEraseBack)
        self.statusbar=self.CreateStatusBar()
        self.all_list={}
        menuBar = wx.MenuBar()
        menu2 = wx.Menu()
        menu2.Append(self.Menu_A_DD, "&Add")
        menu2.Append(self.Menu_Bili,"&BiliBili_Live")
        menu2.AppendSeparator()
        menuBar.Append(menu2, "&Program")
        self.SetMenuBar(menuBar)
        self.Bind(wx.EVT_MENU,self.Menu_Add,id=self.Menu_A_DD)
        self.Bind(wx.EVT_MENU,self.Menu_Bili_,id = self.Menu_Bili)
        '''
        #----------------------------------------------------------------
        bmp = wx.Image("img\\1.bmp", wx.BITMAP_TYPE_BMP).ConvertToBitmap()
        #绘图按钮1，默认风格3D
        self.button = wx.BitmapButton(self.panel, -1, bmp, pos = (20, 10))
        self.all_list["path1"]=self.button
        self.Bind(wx.EVT_BUTTON, self.OnClick, self.button)
        self.button.Bind(wx.EVT_ENTER_WINDOW,self.GameTitle)
        self.button.SetDefault()
        bmp = wx.Image("img\\2.bmp", wx.BITMAP_TYPE_BMP).ConvertToBitmap()
        #绘图按钮1，不带边框
        self.button2 = wx.BitmapButton(self.panel, -1, bmp, pos = (80, 10))
        self.all_list["path2"]=self.button2
        self.Bind(wx.EVT_BUTTON, self.OnClick, self.button2)
        self.button2.Bind(wx.EVT_ENTER_WINDOW,self.GameTitle)
        self.button.SetDefault()
        #----------------------------------------------------------------
        '''
    def AddProgramToMenu(self,filePath):
        print "FileName:",filePath
        tool.SaveingTheICON(filePath,"3")#测试用随便写的
        bmp = wx.Image("img\\3.bmp",wx.BITMAP_TYPE_BMP).ConvertToBitmap()
        print len(self.all_list)
        if len(self.all_list) >= 4:
            y = (len(self.all_list)/4)*50
            x = 20 + (len(self.all_list)%4)*60
            print ">:",x,y
        else:
            if len(self.all_list) == 3:
                y = 5
                x = 200
                print x,y
            if len(self.all_list) == 2:
                y = 5
                x = 140
                print x,y
            if len(self.all_list) == 1:
                y = 5
                x = 80
                print x,y
            if len(self.all_list) == 0:
                y = 5
                x = 20
                print x,y

        Button = wx.BitmapButton(self.panel, -1, bmp, pos = (x, y))
        self.all_list[filePath]=Button
        self.Bind(wx.EVT_BUTTON, self.OnClick, Button)
        Button.Bind(wx.EVT_ENTER_WINDOW,self.GameTitle)
        Button.SetDefault()




#绑定事件集——开始
    def Menu_Bili_(self,event):
        pass
    def Menu_Del(self,event):
        fram = Del_Game()
        fram.Show(True)
    def Menu_Add(self,event):
        file_wildcard = "Program(*.exe)|*.exe|All files(*.*)|*.*"
        dlg = wx.FileDialog(self, "Open paint file...",
                            os.getcwd(),
                            style = wx.OPEN,
                            wildcard = file_wildcard)
        if dlg.ShowModal() == wx.ID_OK:
            filename = dlg.GetPath()
            print "FileName:",filename

            self.AddProgramToMenu(filename)
            frame = Add_Game()
            frame.Show(True)
        dlg.Destroy()
    def GameTitle(self,event):
        for (path,objects) in self.all_list.items():
            if objects == event.GetEventObject():
                self.statusbar.SetStatusText(path)
    def OnClick(self, event):
        #print event.GetEventObject()
        for (path,objects) in self.all_list.items():
            if objects == event.GetEventObject():
                self.statusbar.SetStatusText(path)
                os.system(path.encode("utf-8"))


    def OnEraseBack(self,event):
        dc = event.GetDC()
        if not dc:
            dc = wx.ClientDC(self)
            rect = self.GetUpdateRegion().GetBox()
            dc.SetClippingRect(rect)
        dc.Clear()
        bmp = wx.Bitmap("img\\bg.jpg")
        dc.DrawBitmap(bmp, 0, 0)
    def OnHide(self, event):
        self.Hide()
    def OnIconfiy(self, event):
        self.Hide()
        event.Skip()
    def OnClose(self, event):
        self.taskBarIcon.Destroy()
        self.Destroy()
#绑定事件集——结束
if __name__ == '__main__':
    app = App()
    app.MainLoop()
